package com.sky.business.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.business.sys.domain.ContainerConfig;
import com.sky.business.sys.dto.ContainerConfigDto;
import com.sky.business.sys.param.ContainerConfigParam;
import com.sky.business.sys.service.ContainerConfigService;
import com.sky.common.api.Result;
import com.sky.common.utils.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Tag(name = "容器配置管理", description = "容器配置控制层")
@RequestMapping("/containerConfig")
@Validated
public class ContainerConfigController {

    @Autowired
    private ContainerConfigService containerConfigService;

    @Operation(summary = "容器配置列表")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Map<String,Object>> list(@Schema(description = "镜像名称") @RequestParam(required = false) String imageName,@Schema(description = "页码") @RequestParam Integer page,@Schema(description = "每页记录数") @RequestParam Integer limit) {
        if (page==null || page < 1) {
            page = 1;
        }
        if (limit==null || limit < 1) {
            limit = 10;
        }
        LambdaQueryWrapper<ContainerConfig> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(imageName)){
            wrapper.likeLeft(ContainerConfig::getImageName, imageName);
        }
        IPage<ContainerConfig> iPage  =  containerConfigService.page(new Page<>(page, limit),wrapper);
        List<ContainerConfigDto> recordList  = iPage.getRecords().stream().map(ContainerConfigDto::valueOf).collect(Collectors.toList());
        Map<String, Object> data = new HashMap<>(2);
        data.put("records", recordList);
        data.put("total", iPage.getTotal());
        return Result.success(data);
    }

    @Operation(summary = "添加容器配置")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> add(@RequestBody ContainerConfigParam param) {
        ContainerConfig containerConfig = new ContainerConfig();
        containerConfig.setVersion(param.getVersion());
        containerConfig.setDescription(param.getDescription());
        containerConfig.setImageName(param.getImageName());
        containerConfig.setImageTag(param.getImageTag());
        containerConfig.setImageArch(param.getImageArch());
        containerConfig.setImageSize(param.getImageSize());
        containerConfig.setTaskType(param.getTaskType());
        containerConfig.setSharedMemory(param.getSharedMemory());
        containerConfig.setMemory(param.getMemory());
        containerConfig.setSwapMemory(param.getSwapMemory());
        containerConfig.setKernelMemory(param.getKernelMemory());
        containerConfig.setHostMode(param.getHostMode());
        containerConfig.setRestartPolicy(param.getRestartPolicy());
        containerConfig.setRuntime(param.getRuntime());
        containerConfig.setGpuSupport(param.getGpuSupport());
        containerConfigService.save(containerConfig);

        return Result.success();
    }

    @Operation(summary = "修改容器配置")
    @PostMapping(value = "update", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> update(@RequestBody ContainerConfigParam param) {
        ContainerConfig entity = containerConfigService.getById(param.getId());
        entity.setVersion(param.getVersion());
        entity.setDescription(param.getDescription());
        entity.setImageName(param.getImageName());
        entity.setImageTag(param.getImageTag());
        entity.setImageArch(param.getImageArch());
        entity.setImageSize(param.getImageSize());
        entity.setTaskType(param.getTaskType());
        entity.setSharedMemory(param.getSharedMemory());
        entity.setMemory(param.getMemory());
        entity.setSwapMemory(param.getSwapMemory());
        entity.setKernelMemory(param.getKernelMemory());
        entity.setHostMode(param.getHostMode());
        entity.setRestartPolicy(param.getRestartPolicy());
        entity.setRuntime(param.getRuntime());
        entity.setGpuSupport(param.getGpuSupport());
        containerConfigService.updateById(entity);

        return Result.success();
    }

    @Operation(summary = "删除容器配置")
    @GetMapping(value = "del", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> del(@RequestParam("id") Integer id) {
        containerConfigService.update(new LambdaUpdateWrapper<ContainerConfig>().set(ContainerConfig::getIsDel, true).eq(ContainerConfig::getId, id));
        return Result.success();
    }







}
